from ast import Param
from http.client import responses

import requests
from loguru import logger

from basic.parser import read_file
from const import TOKEN_PATH


def request_hook(r,*args,**kwargs):
    logger.debug(f"接口响应信息为:{r.text}")

class RequestBase:
    def __init__(self):
        self.session = requests.Session()
        self.session.headers.update({'Content-Type': 'application/json', **read_file(str(TOKEN_PATH))})
        self.session.hooks['response'] = request_hook
        self.requests = self.session

    def _post(self,api_name, url : str ,data):
        log_data = {"method": "PSOT ", "url": url, "json": data}
        logger.debug(f"接口{api_name}--请求信息为:{log_data}")
        response = self.requests.post(url, json=data)
        return response.json()

    def _put(self,api_name,url : str,data=None):
        log_data = {"method": "put ", "url": url, "json": data}
        logger.debug(f"接口{api_name}--请求信息为:{log_data}")
        response = self.requests.put(url, json=data)
        return response.json()

    def _get(self,api_name,url:str,params=None):
        log_data = {"method":"GET", "url":url, "pramas":params}
        logger.debug(f"接口{api_name}--请求信息为:{log_data}")
        response = self.requests.get(url, params =params)
        return response.json()
    def _delete(self,url : str):
        pass